import axios from 'axios'
import Message from 'element-ui'

/**
 * 获取地址列表
 */
export const getAddressList = () => {
  try {
    const response = axios.get('http://localhost:3000/address')
    return response
  } catch (error) {
    Message({ type: 'error', message: '获取地址列表失败' })
    return Promise.reject(error)
  }
}
/**
 * 获取区县数据接口方法
 */
export const getAreaList = (pname, cname) => {
  try {
    const response = axios.get(`http://hmajax.itheima.net/api/area?pname=${pname}&cname=${cname}`)
    return response
  } catch (error) {
    Message({ type: 'error', message: '获取区县数据失败' })
    return Promise.reject(error)
  }
}

/**
 * 获取城市数据接口方法
 */
export const getCityList = (pname) => {
  try {
    const response = axios.get(`http://hmajax.itheima.net/api/city?pname=${pname}`)
    return response
  } catch (error) {
    Message({ type: 'error', message: '获取城市数据失败' })
    return Promise.reject(error)
  }
}

/**
 * 获取省份数据接口方法
 */
export const getProvinceList = () => {
  try {
    const response = axios.get('http://hmajax.itheima.net/api/province')
    return response
  } catch (error) {
    Message({ type: 'error', message: '获取省份数据失败' })
    return Promise.reject(error)
  }
}
/**
 * 新增地址
 */
export const addAddress = (data) => {
  try {
    const response = axios.post('http://localhost:3000/address', data)
    return response
  } catch (error) {
    Message({ type: 'error', message: '新增地址失败' })
    return Promise.reject(error)
  }
}

/**
 * 删除地址
 */
export const delAddress = (id) => {
  try {
    const response = axios.delete(`http://localhost:3000/address/${id}`)
    return response
  } catch (error) {
    Message({ type: 'error', message: '删除地址失败' })
    return Promise.reject(error)
  }
}
/**
 * 获取地址详情
 */
export const getAddressDetail = (id) => {
  try {
    const response = axios.get(`http://localhost:3000/address/${id}`)
    return response
  } catch (error) {
    Message({ type: 'error', message: '获取地址详情失败' })
    return Promise.reject(error)
  }
}
/**
 * 修改地址
 */
export const updateAddress = (data) => {
  try {
    const response = axios.put(`http://localhost:3000/address/${data.id}`, data)
    return response
  } catch (error) {
    Message({ type: 'error', message: '修改地址失败' })
    return Promise.reject(error)
  }
}
